package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderVo;
import cn.itcast.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {


    @Reference
    private OrderService orderService;

    /**
     * 分页查询检查组信息
     * @param queryPageBean  行页封装对象
     * @param orderQueryPageBean  条件封装对象
     * @return
     */
    @GetMapping(path = "/findPage")
    public Result findPage(QueryPageBean queryPageBean, OrderQueryPageBean orderQueryPageBean) {
        String orderType = orderQueryPageBean.getOrderType();//获得预约状态
        Integer payStatus = orderQueryPageBean.getPayStatus();//获得支付状态
        String maxTime = orderQueryPageBean.getMaxTime();//获得预约结束时间
        String minTime = orderQueryPageBean.getMinTime();//获得预约开始时间

        try {
        //获得分页数据
        PageInfo<OrderVo> page = orderService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString(), orderType,payStatus, minTime, maxTime);

            if (page == null) {
                //数据为空 没有查到数据
                return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
            }
            //有数据查询成功   将结果封装返回
            PageResult pageResult=new PageResult(page.getTotal(), page.getList());
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    /**
     * 预约列表根据id查询
     * @param id
     * @return
     */
    @GetMapping({"/{id}"})
    public Result findById(@PathVariable("id") String id){
            Order order=orderService.findById(id);

            if (order==null){
                return new Result(false,MessageConstant.QUERY_ORDER_FAIL);

            }
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,order);
    }

    /**
     * 预约添加更新
     * @param order  修改对象
     * @return
     */
    @PostMapping("/submit")
    public Result updateInsert (@RequestBody Order order){
        try {
            if (order.getId()!=null){
                //不为空调用编辑方法 更新
            int  i  =orderService.update(order);
                if (i==0){
                //为0代表影响行数为0 更新失败
                    return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
                }
                // 否者就是成功
                return  new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
            }
            int  count=orderService.insert(order);
            if (count == 0) {
                //如果为0 代表没有影响行数 保存失败
                return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
            }
            // 保存成功
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }


    /**
     * 是否来检查判断
     * @param id
     * @param arrival
     * @return
     */
    @PostMapping("{id}/{arrival}")
    public Result updateById(@PathVariable("id") String id,@PathVariable("arrival") Integer arrival) {
       // System.out.println(arrival);
        int update = orderService.updateArrivalById(id,arrival);
        if (update == 0) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id") String id) {
        int delete = orderService.deleteById(id);
        if (delete == 0) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }



}
